<template>
  <view>
    <view v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)">
      <my-goodsItem :goods="item"></my-goodsItem>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodsQuery: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        isLoading : false
      }
    },
    methods: {
      gotoDetail(item) {
        uni.navigateTo({
          url : '/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id+''
        })
      },
      async getGoodsList(cb) {
        this.isLoading = true
        let {data} = await uni.$http.get('/api/public/v1/goods/search', this.goodsQuery)
        this.isLoading = false
        cb && cb()
        if (data.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList,...data.message.goods]
        this.total = data.message.total
      }
    },
    onLoad(options) {
      this.goodsQuery.query = options.query || ''
      this.goodsQuery.cid = options.cid || ''
      this.getGoodsList()
    },
    onReachBottom() {
      if(this.goodsQuery.pagenum * this.goodsQuery.pagesize >= this.total) return uni.$showMsg('没有更多数据啦!')
      if(this.isLoading) return
      this.goodsQuery.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.goodsQuery.pagenum = 1
      this.goodsList = []
      this.total = 0
      this.isLoading = false
      
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
  
</style>
